/*
 * Copyright (c) Created by T C L on 2019.7.10.
 */

package cn.mumzone.pos.ui.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mumzone.basic.loading.button.LoadingButton;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.ui.adapter.GoodsShopButtonAdapter;
import cn.mumzone.pos.event.DepositGoodsRefreshEvent;
import cn.mumzone.pos.model.DepositGoodsEntity;
import cn.mumzone.pos.model.DepositGoodsRecordEntity;
import cn.mumzone.pos.model.ReceiveDepositGoodEntity;
import cn.mumzone.pos.presenter.DepositReceivePresenter;
import cn.mumzone.pos.presenter.contract.DepositReceiveContract;
import cn.mumzone.pos.ui.widgets.shopping.AnimShopButton;

public class DepositGoodsReceiveDialogFragment extends DialogFragment implements DepositReceiveContract.View {

    @BindView(R.id.tv_good_name)
    TextView tvGoodName;
    @BindView(R.id.tv_good_spec)
    TextView tvGoodSpec;

    @BindView(R.id.asb_receive_goods_count)
    AnimShopButton asbReceiveGoodsCount;

    @BindView(R.id.lb_confirm_receive)
    LoadingButton lbConfirmReceive;

    private int receiveCount = 1;

    private DepositReceivePresenter presenter;

    private DepositGoodsEntity depositGoodsEntity;

    @Override
    public void onStart() {
        super.onStart();
        getDialog().setCanceledOnTouchOutside(false);//点击外部不消失
        // 设置SelectPicPopupWindow弹出窗体的背景
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        params.gravity = Gravity.CENTER;
        params.width = DisplayUtil.dip2px(getActivity(), 359);
        params.height = DisplayUtil.dip2px(getActivity(), 260);
        window.setAttributes(params);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        depositGoodsEntity = (DepositGoodsEntity) getArguments().getSerializable("depositEntity");
        presenter = new DepositReceivePresenter(getActivity(), this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_deposit_goods_receive, container, false);
        ButterKnife.bind(this, view);

        tvGoodName.setText(depositGoodsEntity.getGoodsName());

        StringBuilder goodsSpec = new StringBuilder();
        goodsSpec.append("规格：");
        try {
            if (!TextUtils.isEmpty(depositGoodsEntity.getGoodsSpec())) {
                JSONArray jsonArray = new JSONArray(depositGoodsEntity.getGoodsSpec());
                for (int i = 0; i < jsonArray.length(); i++) {
                    goodsSpec.append(jsonArray.getJSONObject(i).optString("value")).append("，");
                }
                if (goodsSpec.lastIndexOf("，") != -1) {
                    goodsSpec.deleteCharAt(goodsSpec.lastIndexOf("，"));
                } else {
                    goodsSpec.append("无");
                }
                tvGoodSpec.setText(goodsSpec.toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        asbReceiveGoodsCount.setCount(receiveCount);
        asbReceiveGoodsCount.setMaxCount(depositGoodsEntity.getConsignNum() - depositGoodsEntity.getSendNum());
        asbReceiveGoodsCount.setOnAddDelListener(new GoodsShopButtonAdapter(getActivity()) {
            @Override
            protected void onAmountChanged(int count) {
                receiveCount = count;
            }

            @Override
            public void onAddFailed(int count, FailType failType) {
                ToastUtil.makeText(getActivity(), "领取数量不能超过寄存数量~", false);
            }

        });
        return view;
    }

    @OnClick(value = {R.id.iv_close, R.id.lb_confirm_receive})
    public void toggleEvent(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.lb_confirm_receive:
                lbConfirmReceive.startLoading("确认领取");
                List<ReceiveDepositGoodEntity> receiveDepositGoodEntities = new ArrayList<>();
                ReceiveDepositGoodEntity receiveDepositGoodEntity = new ReceiveDepositGoodEntity();
                receiveDepositGoodEntity.setGoodsId(depositGoodsEntity.getGoodsId());
                receiveDepositGoodEntity.setGoodsName(depositGoodsEntity.getGoodsName());
                receiveDepositGoodEntity.setOrderCode(depositGoodsEntity.getOrderCode());
                receiveDepositGoodEntity.setReceiveNum(String.valueOf(receiveCount));
                receiveDepositGoodEntity.setSubCode(depositGoodsEntity.getSubCode());
                receiveDepositGoodEntity.setGoodsSkuId(depositGoodsEntity.getGoodsSkuId());
                receiveDepositGoodEntity.setGoodsSpec(depositGoodsEntity.getGoodsSpec());
                receiveDepositGoodEntities.add(receiveDepositGoodEntity);
                presenter.receiveConsignedGoods(receiveDepositGoodEntities);
                break;
        }
    }

    @Override
    public void setConsignedRecordData(List<DepositGoodsRecordEntity> consignedRecordEntities) {
        //do nothing
    }

    @Override
    public void resolveReceiveSuccess() {
        lbConfirmReceive.stopLoading("确认领取");
        ToastUtil.makeText(getActivity(), "领取成功~", false);
        RxBus.getDefault().post(new DepositGoodsRefreshEvent());
        dismiss();
    }

    @Override
    public void resolveReceiveFailed() {
        lbConfirmReceive.stopLoading("确认领取");
    }
}
